package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    @RequireLogin
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public UserInfo get(Long id){
        return userInfoService.get(id);
    }

    /**
     * 检查手机号是否唯一
     * @param phone
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
         userInfoService.sendVerifyCode(phone);
         return AjaxResult.SUCCESS;
    }

    /**
     * 用户注册
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @PostMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,
                             String rpassword,String verifyCode){
         userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
         return AjaxResult.SUCCESS;
    }

    /**
     * 用户登录
     * @param request
     * @param response
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response,String username, String password){
        String token = userInfoService.userLogin(username,password);
        CookieUtil.setToken(token,response);
        return AjaxResult.SUCCESS;


    }

}
